package com.gongJiao.controller;


import com.gongJiao.Vo.OpinionVo;
import com.gongJiao.Vo.PageCondition;
import com.gongJiao.Vo.PageResult;
import com.gongJiao.common.Result;
import com.gongJiao.entity.Notice;
import com.gongJiao.entity.Opinion;
import com.gongJiao.service.OpinionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 * @author ${author}
 * @since 2021-12-25
 */
@Controller
@RequestMapping("/opinion")
public class OpinionController {

    @Autowired
    OpinionService opinionService;

    /**
     * 后台分页查询意见信息列表
     * @return
     */
    @GetMapping("getOpinionPageList")
    @ResponseBody
    public PageResult<OpinionVo> getVehiclePageList(PageCondition pageCondition){
        PageResult<OpinionVo> pageResult=new PageResult<OpinionVo>();
        //查询所有数据
        List<OpinionVo> list=opinionService.getOpinionList(pageCondition);
        //查询总记录数
        Integer count=opinionService.getCount(pageCondition);
        return pageResult.success(list,count);
    }

    /**
     * 删除意见信息
     * @param ids
     * @return
     */
    @PostMapping("/{ids}")
    @ResponseBody
    public Result deleteOpinionByIds(@PathVariable("ids") String ids){
        opinionService.deleteOpinionByIds(ids);
        return Result.success("意见删除成功！");
    }

    /**
     * 根据id获取意见信息
     * @param id
     * @return
     */
    @GetMapping("/getOpinion/{id}")
    public String getNoticeById(@PathVariable Long id, Model model){
        //获取公告信息
        Opinion opinion =opinionService.getOpinionById(id);
        model.addAttribute("opinion",opinion);
        return "admin/opinionEdit";
    }

    @PostMapping("/addOpinion")
    @ResponseBody
    public Result addOpinion(Opinion opinion){
        if ( opinion.getUserId()==-1){
            return Result.fail("请先登录在发表意见");
        }
        opinion.setCreateTime(new Date());
        opinionService.save(opinion);
        return Result.success("意见发表成功！，感谢您的宝贵意见");
    }


}

